package com.yunlinker.asba.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;


/**
 * @author:Jack Tony
 * @description  :
 * @web :
 * http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
 * http://www.cnblogs.com/kobe8/p/3877125.html
 * 
 * @date  :2015年1月27日
 */
public class BitmapUtils {
	
	public static int suofangSize = 0;
	
	
	/**
	 * @description 计算图片的压缩比率
	 *
	 * @param options 参数
	 * @param reqWidth 目标的宽度
	 * @param reqHeight 目标的高度
	 * @return
	 */
	private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
		// 源图片的高度和宽度
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;
		if (height > reqHeight || width > reqWidth) {
			final int halfHeight = height / 2;
	        final int halfWidth = width / 2;
	        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
	        // height and width larger than the requested height and width.
	        while ((halfHeight / inSampleSize) > reqHeight
	                && (halfWidth / inSampleSize) > reqWidth) {
	            inSampleSize *= 2;
	        }
		}
		return inSampleSize;
	}

	/**
	 * @description 通过传入的bitmap，进行压缩，得到符合标准的bitmap
	 *
	 * @param src
	 * @param dstWidth
	 * @param dstHeight
	 * @return
	 */
	private static Bitmap createScaleBitmap(Bitmap src, int dstWidth, int dstHeight, int inSampleSize) {
		//如果inSampleSize是2的倍数，也就说这个src已经是我们想要的缩略图了，直接返回即可。
		if (inSampleSize % 2 == 0) {
			return src;
		}
		// 如果是放大图片，filter决定是否平滑，如果是缩小图片，filter无影响，我们这里是缩小图片，所以直接设置为false
		Bitmap dst = Bitmap.createScaledBitmap(src, dstWidth, dstHeight, false);
		if (src != dst) { // 如果没有缩放，那么不回收
			src.recycle(); // 释放Bitmap的native像素数组
		}
		return dst;
	}

	/**
	 * @description 从Resources中加载图片
	 *
	 * @param res
	 * @param resId
	 * @param reqWidth
	 * @param reqHeight
	 * @return
	 */
	public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true; // 设置成了true,不占用内存，只获取bitmap宽高
		BitmapFactory.decodeResource(res, resId, options); // 读取图片长款
		options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // 调用上面定义的方法计算inSampleSize值
		// 使用获取到的inSampleSize值再次解析图片
		options.inJustDecodeBounds = false;
		Bitmap src = BitmapFactory.decodeResource(res, resId, options); // 载入一个稍大的缩略图
		return createScaleBitmap(src, reqWidth, reqHeight, options.inSampleSize); // 进一步得到目标大小的缩略图
	}

	/**
	 * @description 从SD卡上加载图片
	 *
	 * @param pathName
	 * @param reqWidth
	 * @param reqHeight
	 * @return
	 */
	public static Bitmap decodeSampledBitmapFromFile(String pathName, int reqWidth, int reqHeight) {
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(pathName, options);
		options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
		options.inJustDecodeBounds = false;
		Bitmap src = BitmapFactory.decodeFile(pathName, options);
		return createScaleBitmap(src, reqWidth, reqHeight, options.inSampleSize);
	}
	
	
	/**
	 * 从网络上的流获取的图片
	 * @param in
	 * @param reqWidth
	 * @param reqHeight
	 * @return 
	 */
	public static synchronized Bitmap decodeSampledBitmapFromStream(  
	        InputStream in,File inputStream, int reqWidth, int reqHeight) {  
	  
	    // First decode with inJustDecodeBounds=true to check dimensions  
	    final BitmapFactory.Options options = new BitmapFactory.Options();  
	    options.inJustDecodeBounds = true;  
	    BitmapFactory.decodeStream(in, null, options);  
//	  
	    
//	    // Calculate inSampleSize  
	    int imSampleSize=calculateInSampleSizeToInputStream(options, reqWidth,reqHeight);  
	    suofangSize = imSampleSize;
//	    if(imSampleSize%2!=0){
//	    	imSampleSize+=1;
//	    }
////	    options.inSampleSize =imSampleSize;
	    Log.e("suofangSize", "suofangSize大小："+suofangSize);
	    try {
			in = new FileInputStream(inputStream);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
	     BitmapFactory.Options options1 = new BitmapFactory.Options();
	     options1.inSampleSize=imSampleSize;
	     options.inPreferredConfig = Bitmap.Config.ARGB_8888;	     
	    // Decode bitmap with inSampleSize set  
	     options1.inJustDecodeBounds = false;  
	    return BitmapFactory.decodeStream(in,null,options1);  
	}  
	
	public static int calculateInSampleSizeToInputStream(BitmapFactory.Options options,  
	        int reqWidth, int reqHeight) {  
	    // Raw height and width of image  
	    final int height = options.outHeight;  
	    final int width = options.outWidth;  
	    
	    Log.e("实际图片的宽："+height, "实际图片的高："+width);
	    int inSampleSize = 1; 
	    
	    int inSampleSize1=0;
	    int inSampleSize2=0;
	  
	    //先根据宽度进行缩小  
	    
	    if(width>=reqWidth){
	    	
	    	inSampleSize1=reqWidth/width;	
	    }
	    
	    if(height>=reqHeight){
	    	
	    	inSampleSize2=reqHeight/height;	
	    }
	    if(inSampleSize1>inSampleSize2){
	    	inSampleSize=inSampleSize1;
	    }else{
	    	inSampleSize=inSampleSize2;
	    }
	   /* while (width / inSampleSize > reqWidth) {  
	        inSampleSize++;  
	    }  
	    //然后根据高度进行缩小  
	    while (height / inSampleSize > reqHeight) {  
	        inSampleSize++;  
	    }  */
	    return inSampleSize;  
	}
	
	
	//计算图片的缩放值  
	public static int newcalculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {  
	    final int height = options.outHeight;  
	    final int width = options.outWidth;  
	    int inSampleSize = 1;  
	  
	    if (height > reqHeight || width > reqWidth) {  
             final int heightRatio = Math.round((float) height/ (float) reqHeight);  
             final int widthRatio = Math.round((float) width / (float) reqWidth);  
             inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;  
	    }  
	        return inSampleSize;  
	} 
	
	// 根据路径获得图片并压缩，返回bitmap用于显示  
	public static Bitmap getSmallBitmap(String filePath,int windWidth,int windHeight) {  
        final BitmapFactory.Options options = new BitmapFactory.Options();  
        options.inJustDecodeBounds = true;  
        BitmapFactory.decodeFile(filePath, options);  
	  
	        // Calculate inSampleSize  
	    options.inSampleSize = calculateInSampleSize(options, windWidth, windHeight);  
	  
	        // Decode bitmap with inSampleSize set  
	    options.inJustDecodeBounds = false;  
	  
	    return BitmapFactory.decodeFile(filePath, options);  
	    } 
	
	
}
